<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <script>
    var initFn = (function(){
      let str = 'qwyashdajh2136sajh23sahgj2hsz2xas310sd11';

      function getNumber(str){
        let arr = [];
        let temp = '';

        for(let i = 0; i < str.length; i++){
          if(!isNaN(str[i])){
            temp += str[i];
          } else {
            if (temp != ''){
              arr.push(temp);
              temp = '';
            }
          }
        }

        if (temp != ''){
          arr.push(temp);
        }

        return arr;
      }

      let arr = getNumber(str);
      console.log(arr);

      let reg = /\d+/g;
      let arr2 = str.match(reg);
      console.log(arr2);
    });
    
    initFn();

    let initFn2 = (function(){
      // 创建方式1 字面量创建 /.../
      // 创建方式2 构造函数创建 new RegExp(a, 'g') => 用于匹配变量

      // 正则方法
      // 1、test 布尔值 2、exec 匹配到的内容

      // 字符串方法
      // 1、split 2、replace 3、search 4、match
      
      // let str = 'ajhds123hxzjh2dks33432kjsjd1';
      // let res = str.split('j');
      // console.log(res);

      let str = '4阿as543克苏较好的权威科asd123技卡机的武器1231啥快递件';
      // let res = str.replace(/较好|武器/g, '**');
      let res = str.replace(/较好|武器123/g, function(e){
        return '*'.repeat(e.length);
      });
      console.log(res);

      // 内容
      let arr = str.match(/\d+/g);
      console.log(arr);

      // 索引值
      let indexs = str.search(/\d+/g);
      console.log(indexs);
    })

    initFn2();

    let initFn3 = (function(){
      // 元字符
      // . * + ? $ ^ | \ () [] {}

      // \n 换行符 \r 回车符 \f 换页符 \s 任意空白字符 \t 制表符
      // . 除了\n \r \u2028 \u2029以外所有字符
      let str = `<div>some value...</div>`;
      let reg = /<div>.*<\/div>/g;
      console.log(str.match(reg));

      // ^ 开头 $ 结尾
      // \w 数字 字母 下划线
      let str1 = 'ad2131_sa_';
      let reg1 = /^a\w*_$/g;
      console.log(reg1.test(str1));

      // []:字符集合 [^ab]: 非
      let str2 = 'hsjdgqwiueyuzdhzxnagjsahdjkqwuljb';
      let reg2 = /a|b/g;
      let newS2 = str2.replace(reg2, '*');
      console.log(newS2);

      // \d === [0-9]
      // . === [^\r\n]
      // \w === [a-zA-Z0-9_]

      // 边界 \b => ^\w => 非数字字母下划线
      let str3 = 'is anywhere';
      let reg3 = /\bis\b/g;
      console.log(reg3.test(str3));

      let str4 = 'My name is LiLei';
      let reg4 = /LiLei/gi;
      console.log(str4.replace(reg4, '李雷'));

      // 转换时间格式 2019-10-19 ----> 19/10/2019;
      let str5 = '2019-10-19&2020-10-20';
      let reg5 = /(\d{4})-(\d{1,2})-(\d{1,2})/g;
      let res = str5.replace(reg5, '$3/$2/$1');
      // console.log(RegExp.$1);
      console.log(res);
      
      // 自定义分组名
      let str7 = '2019-10-19';
      let reg7 = /(?<$ards>\d{4})-(\d{1,2})-(\d{1,2})/g;
      let s7 = str7.replace(reg7, '$3/$2/$ards')
      console.log(s7);
      
      // 断言 es2018 => 有兼容问题
      let str6 = 'iphone4iphone5iphone7iphone11iphoneNumber';
      let reg6 = /iphone(?\d{1,2}})/g
      let res6 = str6.replace(reg6, '苹果');
      console.log(res6);

      // m 多行模式 s 让. 能匹配换行符 y 粘性模式
      // reg.exec(返回一个) == str.match (返回数组)
    })

    initFn3();

    // 华智笔试
    function splitStr(str){
      let split = str.split('_');
      let newStr = '';
      split.forEach((item, index) => {
        if(index != 0){
          item = item.substr(0, 1).toLocaleUpperCase() + item.substr(1);
        }

        newStr += item;
      })
      return newStr;
    }
    let a = splitStr('a_b_asd_kfjw_A');
    console.log(a);

    function replaceStr(str){
      let reg = /_([a-z])/gi;
      let res = str.replace(reg, function(val){
        return val.substr(1).toLocaleUpperCase();
      });
      console.log(res);
    }
    replaceStr('a_b_asd_kfjw_A')
  </script>
</body>
</html>